package com.ivan.stu.notetool.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.style.ImageSpan;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.ScreenUtils;
import com.ivan.stu.notetool.R;
import com.ivan.stu.notetool.View.Loading_view;
import com.ivan.stu.notetool.callback.NegativeCallBack;
import com.ivan.stu.notetool.callback.PositiveCallBack;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;


/**
 * Created by IVAN on 2018/8/9.
 */

public class TextViewUtils {

    private static int ERROR = 0;
    private static int NOTIFY = 1;
    private static int SUCCESS = 2;
    private static Loading_view view;

    public static Toast toast = null;
    /**
     * 富文本添加图片到末尾 并自动匹配高度
     *
     * @param textView
     * @param context
     * @param drawable
     */

    
    public static void addDrawableInEnd(TextView textView, Activity context, Drawable drawable, String str) {

        if (drawable == null) {
            return;
        }

        if (str.length()>40){
            str = str.substring(0,32)+".. ";
        }else {
            str = str+" ";
        }
        TextPaint paint = textView.getPaint();// 获取文本控件字体样式
        Paint.FontMetrics fm = paint.getFontMetrics();
        int textFontHeight = (int) Math.ceil(fm.descent - fm.top) + 2;// 计算字体高度座位图片高度
        int imageWidth = drawable.getIntrinsicWidth() * textFontHeight
                / drawable.getIntrinsicHeight();// 计算图片根据字体大小等比例缩放后的宽度
        drawable.setBounds(0, dip2px(context, 0), drawable.getIntrinsicWidth(),
                drawable.getIntrinsicHeight()+5);// 缩放图片  也可根据实际需求

        ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BOTTOM); //   ImageSpan.ALIGN_BASELINE放置位置
        String space = " ";
        str = str + space;
        int strLength = str.length();
        SpannableString ss = new SpannableString(str);
        ss.setSpan(span, strLength - 1, strLength, Spannable.SPAN_INCLUSIVE_INCLUSIVE);

        // String ellipsizeStr = (String) TextUtils.ellipsize(str, textView.getPaint(), 200, TextUtils.TruncateAt.END);

        //SmileyParser parser = new SmileyParser(this);
        // CharSequence cs = parser.replace(ellipsizeStr);
        textView.setText(ss.subSequence(0, strLength));

    }


    public static String StringFilter(String str) throws PatternSyntaxException {
        str=str.replaceAll(" ","").replaceAll(" ","").replaceAll("：",":").replaceAll("：","：").replaceAll("【","[").replaceAll("】","]").replaceAll("！","!");//替换中文标号
        String regEx="[『』]"; // 清除掉特殊字符
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.replaceAll("").trim();
    }

    public static String toSBC(String input) { //半角转全角：
        char[] c = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == 32) {
                c[i] = (char) 12288;
                continue;
            }
            if (c[i] < 127) c[i] = (char) (c[i] + 65248);
        }
        return new String(c);
    }


    public static String ToDBC(String input) {
        char[] c = input.toCharArray();
        for (int i = 0; i< c.length; i++) {
            if (c[i] == 12288) {
                c[i] = (char) 32;
                continue;
            }if (c[i]> 65280&& c[i]< 65375)
                c[i] = (char) (c[i] - 65248);
        }
        return new String(c);
    }

    public static int dip2px(Context context, float value) {

        return (int) (context.getResources().getDisplayMetrics().density * value + 0.5f);
    }


    @SuppressLint("ResourceAsColor")
    public static void showDialog(Context context, String content, final PositiveCallBack positiveCallBack, final NegativeCallBack negativeCallBack){
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View view = View
                .inflate(context, R.layout.notify_dialog_layout, null);

        TextView textView = view.findViewById(R.id.tv_msg_content);
        textView.setText(content);
        final Button bt_cancel = view.findViewById(R.id.bt_cancel);
        final Button bt_confirm = view.findViewById(R.id.bt_confirm);

        bt_confirm.setSelected(true);
        bt_cancel.setSelected(false);
        builder.setView(view);
        builder.setCancelable(false);

        //取消或确定按钮监听事件处理
        final AlertDialog dialog = builder.create();
        dialog.getWindow().setDimAmount(0.2f);//设置昏暗度为0

        //   Window window=dialog.getWindow();
        //  window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        //  dialog.getWindow().setGravity(Gravity.CENTER);


        dialog.show();

        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();

        if (ConstrantManager.currentSystemid.equals("b30")){

            params.width = (int) (ScreenUtils.getScreenWidth()*0.7);
        }else {

            params.width = (int) (ScreenUtils.getScreenWidth()*0.9);
        }

        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.CENTER;

        dialog.getWindow().setAttributes(params);

        bt_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                bt_confirm.setSelected(false);
                bt_cancel.setSelected(true);
                negativeCallBack.negativeCallBack(dialog);

            }
        });

        bt_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                bt_confirm.setSelected(true);
                bt_cancel.setSelected(false);
                positiveCallBack.positiveCallBack(dialog);

            }
        });

    }


    public static void showProgressDialog(Context context,Class clz,String msg) {

        //Loading_view view = new Loading_view(context,R.style.CustomDialog);
        if (view!=null){

            if (ActivityCollectorUtils.isActivityExist(clz)){

                view.setMsg(msg);

                view.show();

            }

        }else {

            view = new Loading_view(context,R.style.CustomDialog);

            view.setMsg(msg);

            if (ActivityCollectorUtils.isActivityExist(clz)){

                view.show();

            }

        }

    }


    public static void clearProgressDialog(){

        if (view!=null){

            view.dismiss();
            view = null;
        }
    }

    public static void hideProgressDialog(Context context) {
        // Loading_view view = new Loading_view(context,R.style.CustomDialog);

        if (view!=null){

            view.dismiss();

        }else {

            view = new Loading_view(context,R.style.CustomDialog);
            view.dismiss();

        }
    }

    public static void showToast(Context context,int type,String content){


        View toastRoot = LayoutInflater.from(context).inflate(R.layout.item_toast, null);

        if (toast == null) {

            toast = new Toast(context);


        }




        toast.setView(toastRoot);
        TextView tv = (TextView) toastRoot.findViewById(R.id.tv_toast);
        ImageView iv = toastRoot.findViewById(R.id.iv_toast_icon);

        switch (type){
            case 0 :

                iv.setBackgroundResource(R.mipmap.notetool_fail_icon);
                break;
            case 1 :

                iv.setBackgroundResource(R.mipmap.notetool_notify_icon);
                break;
            case 2 :

                iv.setBackgroundResource(R.mipmap.notetool_success_icon);
                break;
            default:
                break;

        }

        tv.setText(content);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();


    }
}